/**
 * 
 */
package org.t2.game.server.module.player.service;

import org.gaming.ruler.eventbus.Subscribe;
import org.gaming.service.repository.IRepository;
import org.gaming.service.repository.Repository;
import org.gaming.tool.DateTimeUtil;
import org.gaming.tool.LocalDateTimeUtil;
import org.springframework.stereotype.Component;
import org.t2.game.server.module.player.event.PlayerCreateEvent;
import org.t2.game.server.module.player.event.PlayerLoginEvent;
import org.t2.game.server.module.player.model.Player;
import org.t2.game.server.module.recharge.event.RechargeEvent;

import sh.t2.share.module.player.log.PlayerCreateLog;
import sh.t2.share.module.player.log.PlayerLoginLog;
import sh.t2.share.module.player.log.PlayerRechargeLog;

/**
 * 
 */
@Component
public class PlayerLogger {

	private IRepository<PlayerLoginLog> loginRepository;
    private IRepository<PlayerCreateLog> createRepository;
    private IRepository<PlayerRechargeLog> rechargeRepository;
    

    private IRepository<PlayerLoginLog> loginRepository() {
        if (loginRepository == null) {
            loginRepository = Repository.get(PlayerLoginLog.class);
        }
        return loginRepository;
    }

    private IRepository<PlayerCreateLog> createRepository() {
        if (createRepository == null) {
            createRepository = Repository.get(PlayerCreateLog.class);
        }
        return createRepository;
    }
    
    private IRepository<PlayerRechargeLog> rechargeRepository() {
        if (rechargeRepository == null) {
        	rechargeRepository = Repository.get(PlayerRechargeLog.class);
        }
        return rechargeRepository;
    }
	
	@Subscribe
    private void listen(PlayerLoginEvent event) {
		PlayerLoginLog loginLog = formLoginLog(event.getPlayer());
        loginRepository().insert(loginLog);
    }

    @Subscribe
    private void listen(PlayerCreateEvent event) {
        Player player = event.getPlayer();
        PlayerCreateLog createLog = formCreateLog(player);
        createRepository().insert(createLog);
        
        PlayerLoginLog loginLog = formLoginLog(player);
        loginRepository().insert(loginLog);
    }
    
    protected PlayerCreateLog formCreateLog(Player player) {
    	PlayerCreateLog log = new PlayerCreateLog();
    	log.setChannelId(player.getChannelId());
    	log.setPlayerId(player.getId());
    	log.setOpenId(player.getOpenId());
    	log.setNick(player.getNick());
    	log.setBornTime(LocalDateTimeUtil.toEpochMilli(player.getInsertTime()));
    	log.setIp(player.getCreateIp());
    	log.setBornDate(Integer.parseInt(DateTimeUtil.formatMillis(DateTimeUtil.YYYYMMDD, log.getBornTime())));
    	log.setNewComer(player.isNewComer());
    	return log;
    }
    
    protected PlayerLoginLog formLoginLog(Player player) {
        PlayerLoginLog log = new PlayerLoginLog();
        log.setChannelId(player.getChannelId());
        log.setPlayerId(player.getId());
        log.setOpenId(player.getOpenId());
        log.setNick(player.getNick());
        log.setLevel((int)player.getLevel());
        log.setLoginTime(player.getLastLoginTime());
        log.setBornTime(LocalDateTimeUtil.toEpochMilli(player.getInsertTime()));
        log.setIp(player.getLoginIp());
        log.setCreatePay(player.isCreatePay() ? 1 : 0);
        log.setLoginDate(Integer.parseInt(DateTimeUtil.formatMillis(DateTimeUtil.YYYYMMDD, log.getLoginTime())));
    	log.setBornDate(Integer.parseInt(DateTimeUtil.formatMillis(DateTimeUtil.YYYYMMDD, log.getBornTime())));
    	if(player.getFirstPayTime() > 0) {
    		log.setFirstPayDate(Integer.parseInt(DateTimeUtil.formatMillis(DateTimeUtil.YYYYMMDD, player.getFirstPayTime())));
    	}
    	return log;
    }
    
    
    @Subscribe
    private void listen(RechargeEvent event) {
    	Player player = event.getPlayer();
    	PlayerRechargeLog log = formRechargeLog(player, DateTimeUtil.currMillis(), event.getPayMoney());
    	rechargeRepository().insert(log);
    	
    	if(event.isCreatePay() || event.isFirstPay()) {
    		//创号当天有充值或者首次充值，都需要追加一条登录日志用于统计付费留存
    		PlayerLoginLog loginLog = formLoginLog(player);
            loginRepository().insert(loginLog);
    	}
    }
    
    protected PlayerRechargeLog formRechargeLog(Player player, long rechargeTime, long money) {
    	PlayerRechargeLog log = new PlayerRechargeLog();
    	log.setChannelId(player.getChannelId());
    	log.setPlayerId(player.getId());
    	log.setRechargeTime(rechargeTime);
    	log.setBornTime(LocalDateTimeUtil.toEpochMilli(player.getInsertTime()));
    	log.setRechargeDate(Integer.parseInt(DateTimeUtil.formatMillis(DateTimeUtil.YYYYMMDD, log.getRechargeTime())));
    	log.setBornDate(Integer.parseInt(DateTimeUtil.formatMillis(DateTimeUtil.YYYYMMDD, log.getBornTime())));
    	log.setMoney(money);
    	log.setNewComer(player.isNewComer());
    	return log;
    }
}
